/***************************************************************************
 创建者: hualei
 开始时间: 2018.9.10
 copyright: (C) 深圳市华友高科有限公司
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018.9.10 类的具体实现代码编写

 ***************************************************************************/
#include "ftpdelegate.h"
#include "../FtpManage/ftpmanage.h"
#include <QDebug>
#include "../CommunicateDelegate/filedata.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"


extern FileData fileData;

extern FtpManage *ftpManage;

FtpDelegate::FtpDelegate(QObject *parent) : QObject(parent)
{

    connect(ftpManage, SIGNAL(isLogined(bool,QString)), this, SLOT(isLoginToFtpSuccessSlot(bool,QString)));
    connect(ftpManage, SIGNAL(getFileInServerDirectory(QHash<QString,bool>,QString,QString)),
            this,  SLOT(getFileInServerDirectorySlot(QHash<QString,bool> , QString, QString)));
    connect(ftpManage, SIGNAL(isGetFile(bool,QString)), this, SLOT(getFileSlot(bool,QString)));
//    connect(ftpManage, SIGNAL(isGetDirectoryFile(bool,QString)), this, SLOT(listSlot(bool,QString)));

    // 下载地图目录下所有文件
    serverClientPathList[D_MAP_PATH]     = D_MAP_PATH;
    // 下载机器人图片目录下所有文件
    serverClientPathList[D_ROBOT_IMAGE_PATH] = D_ROBOT_IMAGE_PATH;

    // 不使用gsoap时，需要ftp下载的文件
    if(!GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        // 程序文件
        serverClientPathList[PROGRAM_PATH] = PROGRAM_PATH;
        // 配置文件
        serverClientPathList[D_CONFIG_PATH]  = D_CONFIG_PATH;
    }

    currentDirIndex = 0;
    currentFileIndex = 0;

    ftpManage->setGetDirFlag(false);
}

FtpDelegate::~FtpDelegate()
{

}

bool FtpDelegate::isLoginToFtpSuccess()
{
    return isLoginToFtp;
}

void FtpDelegate::loginToFtpSlot(const QString &ctrlIp, int port, const QString &userName, const QString &userPasswd)
{
    QString serverIp = ctrlIp;

    // 查找 ctrlIp 字符串中 ":" 的位置，截取其中的 IP 部分
    serverIp = serverIp.left(serverIp.indexOf(":"));
    ftpManage->ftpLogin(serverIp, port, userName, userPasswd);
}

void FtpDelegate::isLoginToFtpSuccessSlot(bool result, QString message)
{
    isLoginToFtp = result;
    if(result)
    {
//        ftpManage->loginGetDirFlag = true;
//        ftpManage->setLoginFileGetFlag(true);

//        fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
//        ftpManage->getFileInDirectory(serverClientPathList.keys().at(currentDirIndex),
//                                      serverClientPathList.values().at(currentDirIndex));
    }
}

void FtpDelegate::getFileInServerDirectorySlot(QHash<QString, bool> fileList, QString serverPath, QString clientPath)
{
    // 当下载地图文件时立即返回，避免因调用同一信号时响应多个槽函数
//    if(fileData.ftpOperateNum != EM_FTP_OPERATE_LOGIN
//            || fileData.ftpOperateNum != EM_FTP_OPERATE_MUSIC
//            || fileData.ftpOperateNum != EM_FTP_OPERATE_LOAD_MAP)
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_LOGIN)
<<<<<<< HEAD
    {
        return;
    }

    serverFileNameList.clear();

    //当访问目录为空时,跳至下载下一个目录
    if(fileList.isEmpty())
=======
>>>>>>> dev
    {
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        currentDirIndex++;
        if(currentDirIndex < serverClientPathList.keys().count())
        {
            serverFileNameList.clear();
            currentFileIndex = 0;
//                ftpManage->setGetDirFlag(true);
            fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
            ftpManage->getFileInDirectory(serverClientPathList.keys().at(currentDirIndex), serverClientPathList.values().at(currentDirIndex));
        }
        else
        {
            currentDirIndex = 0;
            currentFileIndex = 0;
            fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
//                ftpManage->setGetDirFlag(false);
            emit loginGetDirFinished();
        }
        return;
    }
<<<<<<< HEAD
=======
    // 文件模块使用标识
//    if(fileData.fileManageFlag)
//    {
//        return;
//    }
//    if(fileData.getMapFileInfoFlag)
//    {
//        return;
//    }
//    if(fileData.getMusicFileInfoFlag)
//    {
//        return;
//    }
//    if(!ftpManage->isLoginDirFlag())                // 是否时下载登录文件 true
//    {
//        return;
//    }
    serverFileNameList.clear();

    //当访问目录为空时,跳至下载下一个目录
>>>>>>> dev

    for(int i = 0; i < fileList.values().count(); i++)
    {
        if(fileList.values().at(i) == false)
        {
            serverFileNameList << fileList.keys().at(i);
        }
    }
    serverPathTmp = serverPath;
    clientPathTmp = clientPath;
//    ftpManage->setGetDirFlag(true);
    currentFileIndex = 0;
<<<<<<< HEAD
    fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
    ftpManage->ftpGetFile(serverFileNameList.at(currentFileIndex), clientPath, serverPath);
=======

    // 如果下载文件目录为空,就跳转下载下一个目录
    if(serverFileNameList.isEmpty())
    {
        // 关闭FTP操作标识
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        currentDirIndex++;
        // 如果当前目录索引小于要下载的目录数,就下载下一个目录
        if(currentDirIndex < serverClientPathList.keys().count())
        {
            serverFileNameList.clear();
            currentFileIndex = 0;
//                ftpManage->setGetDirFlag(true);
            fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
            ftpManage->getFileInDirectory(serverClientPathList.keys().at(currentDirIndex), serverClientPathList.values().at(currentDirIndex));
        }
        // 目录下载完成
        else
        {
            currentDirIndex = 0;
            currentFileIndex = 0;
            serverFileNameList.clear();
            fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
//                ftpManage->setGetDirFlag(false);
            // 更改为先登录验证,登录成功再FTP下载，故已废弃
            //emit loginGetDirFinished();
        }
        return;
    }
    else    // 不为空,执行下载
    {
        qDebug()<<"serverFileNameList is full";
//         if(ftpManage->isDownLoadFileStatus())
//         {
             fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
             ftpManage->ftpGetFile(serverFileNameList.at(currentFileIndex), clientPath, serverPath);
             //201912255555
//         }

    }

>>>>>>> dev
}

void FtpDelegate::getFileSlot(bool result, QString message)
{
//    qDebug() << "ftpManage->isLoginDirFlag() :" << ftpManage->isLoginDirFlag() << " =" << result;
    qDebug() << " ========== " << fileData.ftpOperateNum;
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_LOGIN)
    {
        return;
    }

//    if(!ftpManage->isLoginDirFlag())        // 是否是下载登录文件 true
//    {
//        return;
//    }

//    if(fileData.fileManageFlag)
//    {
//        return;
//    }
    if(result)
    {
//        qDebug() << "ftpManage->isGetDir() :" << ftpManage->isGetDir();
//        if(!ftpManage->isGetDir())          // 是否是下载目录  true
//        {
//            return;
//        }
        currentFileIndex++;
        if(currentFileIndex < serverFileNameList.count())
        {
            fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
            qDebug() << "Ftpdelegate : Downloading Login File ===========================";
            ftpManage->ftpGetFile(serverFileNameList.at(currentFileIndex), clientPathTmp, serverPathTmp);
        }
        else
        {
            fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
            currentDirIndex++;
            if(currentDirIndex < serverClientPathList.keys().count())
            {
                serverFileNameList.clear();
                currentFileIndex = 0;
//                ftpManage->setGetDirFlag(true);
                fileData.ftpOperateNum = EM_FTP_OPERATE_LOGIN;
//                ftpManage->getFileInDirectory(serverClientPathList.keys().at(currentDirIndex), serverClientPathList.values().at(currentDirIndex));
            }
            else
            {
                currentDirIndex = 0;
                currentFileIndex = 0;
                fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
//                ftpManage->setGetDirFlag(false);
<<<<<<< HEAD
                emit loginGetDirFinished();
=======
                // 更改为先登录验证,登录成功再FTP下载，故已废弃
                //emit loginGetDirFinished();
>>>>>>> dev
            }
        }
    }
    else
    {
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        qDebug() << " ********************  Download Login File List Failed! *********************";
        return;
    }
}
